package com.yeyi.ytest.java8.stream;

import java.util.List;
import java.util.Optional;

import com.yeyi.ytest.java8.Article;
import com.yeyi.ytest.java8.ArticleUtil;

public class StreamTest {
    
    public Article getFirstArticleFor(List<Article> articles) {
        
        for (Article article : articles) {
            if (article.getTags().contains("Java")) {
                return article;
            }
        }
     
        return null;
    }
    
    public Optional<Article> getFirstArticleStream(List<Article> articles) {  
        return articles.stream()
                .filter(article -> article.getTags().contains("Java"))
                .findFirst();
    }
    
    /**
     * 
     * @param length 创建数组长度
     * @param isArrayList true使用 ArrayList false使用LinkedList 
     * @param isFor true 使用
     */
    void spendTimeTest(int length, boolean isArrayList, boolean isFor) {
        List<Article> arrayList = ArticleUtil.randomArticles(length, isArrayList);
        long startTime = 0;
        long endTime = 0;
        Article a = null;
        if(isFor) {
            startTime = System.currentTimeMillis();
            getFirstArticleFor(arrayList);
            endTime = System.currentTimeMillis();
        }else {
            startTime = System.currentTimeMillis();
            getFirstArticleStream(arrayList);
            endTime = System.currentTimeMillis();
        }
        System.out.println("length: "+length+" isArrayList: "+isArrayList+" spendTime:"+(endTime-startTime)+" isFor: "+isFor);
        System.out.println("result: "+a);
    }
    
    public static void main(String []argv) {
        StreamTest st = new StreamTest();
        int arrayLength = 1000;
        st.spendTimeTest(arrayLength, true, true);
        st.spendTimeTest(arrayLength, true, false);
        st.spendTimeTest(arrayLength, false, true);
        st.spendTimeTest(arrayLength, false, false);
        
        System.out.println();
        st.spendTimeTest(arrayLength, true, true);
        st.spendTimeTest(arrayLength, true, false);
        st.spendTimeTest(arrayLength, false, true);
        st.spendTimeTest(arrayLength, false, false);
        
        System.out.println();
        arrayLength = 10000;
        st.spendTimeTest(arrayLength, true, true);
        st.spendTimeTest(arrayLength, true, false);
        st.spendTimeTest(arrayLength, false, true);
        st.spendTimeTest(arrayLength, false, false);
        
        System.out.println();
        st.spendTimeTest(arrayLength, true, true);
        st.spendTimeTest(arrayLength, true, false);
        st.spendTimeTest(arrayLength, false, true);
        st.spendTimeTest(arrayLength, false, false);
        
        System.out.println();
        arrayLength = 100000;
        st.spendTimeTest(arrayLength, true, true);
        st.spendTimeTest(arrayLength, true, false);
        st.spendTimeTest(arrayLength, false, true);
        st.spendTimeTest(arrayLength, false, false);
        
        System.out.println();
        st.spendTimeTest(arrayLength, true, true);
        st.spendTimeTest(arrayLength, true, false);
        st.spendTimeTest(arrayLength, false, true);
        st.spendTimeTest(arrayLength, false, false);
    }
}
// 结论:
// 一、在第一次执行时, stream 比 for 慢很多,但从第二次开始后基本一样
// 二、遍历的时候，ArrayList 比 LinkedList 快一点，但不是很多。

// 结果:
//length: 1000 isArrayList: true spendTime:2 isFor: true
//result: null
//length: 1000 isArrayList: true spendTime:46 isFor: false
//result: null
//length: 1000 isArrayList: false spendTime:2 isFor: true
//result: null
//length: 1000 isArrayList: false spendTime:1 isFor: false
//result: null
//
//length: 1000 isArrayList: true spendTime:0 isFor: true
//result: null
//length: 1000 isArrayList: true spendTime:0 isFor: false
//result: null
//length: 1000 isArrayList: false spendTime:1 isFor: true
//result: null
//length: 1000 isArrayList: false spendTime:1 isFor: false
//result: null
//
//length: 10000 isArrayList: true spendTime:3 isFor: true
//result: null
//length: 10000 isArrayList: true spendTime:5 isFor: false
//result: null
//length: 10000 isArrayList: false spendTime:12 isFor: true
//result: null
//length: 10000 isArrayList: false spendTime:11 isFor: false
//result: null
//
//length: 10000 isArrayList: true spendTime:2 isFor: true
//result: null
//length: 10000 isArrayList: true spendTime:5 isFor: false
//result: null
//length: 10000 isArrayList: false spendTime:9 isFor: true
//result: null
//length: 10000 isArrayList: false spendTime:13 isFor: false
//result: null
//
//length: 1000000 isArrayList: true spendTime:121 isFor: true
//result: null
//length: 1000000 isArrayList: true spendTime:143 isFor: false
//result: null
//length: 1000000 isArrayList: false spendTime:187 isFor: true
//result: null
//length: 1000000 isArrayList: false spendTime:181 isFor: false
//result: null
//
//length: 1000000 isArrayList: true spendTime:122 isFor: true
//result: null
//length: 1000000 isArrayList: true spendTime:125 isFor: false
//result: null
//length: 1000000 isArrayList: false spendTime:189 isFor: true
//result: null
//length: 1000000 isArrayList: false spendTime:163 isFor: false
//result: null
